package com.sinodata.bsm.center.service.notify;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.sinodata.bsm.center.dao.BaseDao;
import com.sinodata.bsm.center.dao.impl.NotifyDaoImpl;
import com.sinodata.bsm.common.vo.Notify;
import com.sinodata.bsm.common.vo.NotifyHis;

/**
 * <p>
 * Description: 通知服务类
 * </p>
 *
 * @author liulibo
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-5-25 PM 3:47:24     liulibo        1.0         To create
 * </p>
 *
 * @since 
 * @see     
 */
@Service
@Transactional(propagation = Propagation.NOT_SUPPORTED)
public class NotifyService {

    @Autowired
    private BaseDao baseDao;

    @Autowired
    private NotifyDaoImpl notifyDao;

    /**
     * 产生事件通知入库，发送后清除
     * 
     * @param notify
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public void add(Notify notify) {
        baseDao.save(notify);
    }

    /**
     * 删除通知，适用于暂停时，删除待发送通知
     * 
     * @param notify
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public void delete(Notify notify) {
        baseDao.delete(notify);
    }

    /**
     * 产生事件通知入库，发送后清除(批量方法)
     * 
     * @param notify
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public void batchAdd(List<Notify> list) throws Exception {
        baseDao.batchSave(list);
    }

    /**
     * 发送事件通知，清除发送表数据，同时加入发送通知历史表
     * 
     * @param notify
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public void notify(Notify notify) {
        NotifyHis notifyHis = new NotifyHis();
        notifyHis.setEventId(notify.getEventId());
        notifyHis.setMsg(notify.getMsg());
        notifyHis.setNotifyMode(notify.getNotifyMode());
        notifyHis.setTime(new Date());
        notifyHis.setUser(notify.getUser());
        baseDao.delete(notify);
        baseDao.save(notifyHis);
    }

    /**
     * 批量发送事件通知，清除发送表数据，同时加入发送通知历史表
     * 
     * @param notify
     * @throws Exception 
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public void batchNotify(List<Notify> notifys) throws Exception {
        List<NotifyHis> list = new ArrayList<NotifyHis>();
        Long[] ids = new Long[notifys.size()];
        int i = 0;
        for (Notify notify : notifys) {
            NotifyHis notifyHis = new NotifyHis();
            notifyHis.setEventId(notify.getEventId());
            notifyHis.setMsg(notify.getMsg());
            notifyHis.setNotifyMode(notify.getNotifyMode());
            notifyHis.setTime(notify.getTime());
            notifyHis.setUser(notify.getUser());
            list.add(notifyHis);
            ids[i] = notify.getId();
            i++;
        }
        notifyDao.batchDelete(ids);
        baseDao.batchSave(list);
    }

    /**
     * 查询准备发送的通知
     * 按时间从小到大排序
     * 
     */
    public List<Notify> findNotifyForSend() {
        String hql = "from Notify order by time";
        return (List<Notify>) baseDao.findListByHql(hql);
    }

}